#include <Arduino.h>
#include <ALIBEBuzzer.h>
#include <ALIBEpitches.h>

	ALIBEBuzzer::ALIBEBuzzer(){
	//
	_mute=false;
	}
	
	bool ALIBEBuzzer::systemOn(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_G3, NOTE_C4 };
		int playDuration[]= { 8, 4};
		_play(playNote, playDuration, 2);
	}

	bool ALIBEBuzzer::systemOff(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_B3, NOTE_C4 };
		int playDuration[]= { 8, 4};
		_play(playNote, playDuration, 2);
	}

	bool ALIBEBuzzer::commandIn(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_B3, NOTE_C4 };
		int playDuration[]= { 4, 4};
		_play(playNote, playDuration, 2);
	}
	
	bool ALIBEBuzzer::commandOut(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_A3, NOTE_G3 };
		int playDuration[]= { 4, 4};
		_play(playNote, playDuration, 2);
	}
	
	
	bool ALIBEBuzzer::obstacleInFrontRange(int range){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_A3 + range, NOTE_G3 + range};
		int playDuration[]= { 4, 4};
		_play(playNote, playDuration, 2);
	}
	
	
	bool ALIBEBuzzer::obstacleInRearRange(int range){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_G3 + range, NOTE_A3 + range};
		int playDuration[]= { 4, 4};
		_play(playNote, playDuration, 2);
	}
	
	bool ALIBEBuzzer::transmissionBWD(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_B3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	
	bool ALIBEBuzzer::transmissionStop(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_C4};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	
	bool ALIBEBuzzer::transmissionFWD(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_G3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	bool ALIBEBuzzer::lowBattery(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_A3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	
	bool ALIBEBuzzer::gpsSearching(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = { NOTE_A3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	
	bool ALIBEBuzzer::gpsLocked(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3};
		int playDuration[]= { 8,8,8,8 };
		_play(playNote, playDuration, 4);
	}

	
	bool ALIBEBuzzer::turningRight(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_G3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	
	bool ALIBEBuzzer::turningLeft(){
		// int playNote[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
		int playNote[] = {NOTE_G3};
		int playDuration[]= { 8 };
		_play(playNote, playDuration, 1);
	}

	bool ALIBEBuzzer::muteOn(){
		_mute=true;
	}
	
	bool ALIBEBuzzer::muteOff(){
		_mute=false;
	}
	
	//	private:
	bool ALIBEBuzzer::_play(int _notes[], int _duration[], int arraySize){
	  if (_mute==true) return false;
	  
	  // iterate over the notes of the melody:
	  for (int thisNote = 0; thisNote < arraySize; thisNote++) {
		// to calculate the note duration, take one second 
		// divided by the note type.
		//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
		int noteDuration = 1000/_duration[thisNote];
		tone(BUZZER_PIN, _notes[thisNote],noteDuration);

		// to distinguish the notes, set a minimum time between them.
		// the note's duration + 30% seems to work well:
		int pauseBetweenNotes = noteDuration * 1.30;
		delay(pauseBetweenNotes);
		// stop the tone playing:
		noTone(BUZZER_PIN);
		}
	}

	
